/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/
#include "../room.h"

#include "core/user.h"
#include "core/waypoint.h"

#include <iostream>
#include <assert.h>

using namespace greenhills::core;

CRoom::CRoom(const CRoomId &pRoomId, const std::string &pRoomName)
{
    mRoomId = pRoomId;
    mRoomName = pRoomName;
}

bool CRoom::isEmpty() const
{
    return mRoomItems.empty();
}

IRoomItem *CRoom::item(const CItemId &pItemId) const
{
    if (mRoomItems.count(pItemId) == 0)
        return 0;

    return mRoomItems[pItemId];
}

IRoomItem *CRoom::itemAt(int index) const
{
    ConstItemIterator it;
    for (it = begin(); it != end(); it++)
    {
        if (index==0)
            return (*it).second;
        index--;
    }
    return 0;
}

int CRoom::indexOf(const CItemId &pItemId) const
{
    ConstItemIterator it;
    int index = 0;
    for (it = begin(); it != end(); it++)
    {
        if ((*it).second->id() == pItemId)
            return index;
        index++;
    }
    return -1;
}

int CRoom::count() const
{
    return mRoomItems.size();
}

CRoom::ItemIterator CRoom::begin() const
{
    return mRoomItems.begin();
}

CRoom::ItemIterator CRoom::end() const
{
    return mRoomItems.end();
}

greenhills::core::IRoomItem *CRoom::createItem(
        const greenhills::core::CItemId &pItemId,
        const std::string &pItemName,
        const greenhills::core::CItemType &pItemType)
{
    switch(pItemType)
    {
    case CItemType::itUser:
        return new CUser(pItemId, pItemName);
        break;
    case CItemType::itWaypoint:
        return new CWaypoint(pItemId, pItemName);
        break;
    default:
        Q_ASSERT(false);
        break;
    }
    return 0;
}

bool CRoom::addItem(const CItemId &pItemId,
                         const std::string &pItemName,
                         const CItemType &pItemType)
{
    IRoomItem *item = createItem(pItemId, pItemName, pItemType);
    if (!item)
        return false;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->aboutToAddItem(this, pItemId);
    }

    mRoomItems[pItemId] = item;
    std::cout << "core::CRoom: Item added (Id:" <<
            pItemId << ")" << std::endl;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->itemAdded(this, pItemId, pItemName, pItemType);
    }

    return true;
}

bool CRoom::removeItem(const CItemId &pItemId)
{
    if (item(pItemId) == 0)
        return false;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->aboutToRemoveItem(this, pItemId);
    }

    mRoomItems.erase(pItemId);
    std::cout << "core::CRoom: Item removed (Id:" <<
            pItemId << ")" << std::endl;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->itemRemoved(this, pItemId);
    }

    return true;
}

bool CRoom::updateItem(const CItemId &pItemId,
                        const unsigned char *pData,
                        unsigned int pLength)
{
    core::IRoomItem *roomItem = CRoom::item(pItemId);
    if (roomItem == 0)
        return false;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->aboutToUpdateItem(this, pItemId);
    }
    bool rValue = roomItem->updateItem(pData, pLength);
    std::cout << "core::CRoom: Item updated (Id:" <<
            pItemId << ")" << std::endl;

    {
        std::vector<core::IRoomListener*>::iterator it;
        for (it = mListeners.begin(); it != mListeners.end(); it++)
            (*it)->itemUpdated(this, pItemId, pData, pLength);
    }

    return rValue;
}

void CRoom::registerListener(IRoomListener *pListener)
{
    assert(pListener!=NULL);

    std::vector<core::IRoomListener*>::iterator it;

    for (it = mListeners.begin(); it != mListeners.end(); it++) {
        if (*it == pListener) {
            return;
        }
    }

    mListeners.push_back(pListener);
}

void CRoom::unregisterListener(IRoomListener *pListener)
{
    std::vector<core::IRoomListener*>::iterator it;

    for (it = mListeners.begin(); it != mListeners.end(); it++) {
        if (*it == pListener) {
            mListeners.erase(it);
            break;
        }
    }
}

void CRoom::unregisterAllListeners()
{
    mListeners.clear();
}
